<?php

class AnnouncementsController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session', 'Util', 'Upload');
  public $name = 'Announcements';
  public $visibility = array('P' => 'Public', 'R' => 'Private', 'H' => 'Hidden');
  public $paginate;

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'Announcement.modified' => 'desc'
            ));
  }

//admin and public version
  function member_index() {
    $this->set('announcements', $this->paginate('Announcement'));
    $this->set('visibilityOptions', $this->visibility);
  }


  //admin
  function member_add() {
    $this->set('visibilityOptions', $this->visibility);
    $test = $this->Announcement->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }

  //admin
  function member_edit($id = null) {
    $this->set('visibilityOptions', $this->visibility);
    $this->Announcement->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Announcement->read();
      $this->request->data['Announcement']['start_date'] = $this->Util->formatDate($this->request->data['Announcement']['start_date'], true);
      $this->request->data['Announcement']['end_date'] = $this->Util->formatDate($this->request->data['Announcement']['end_date'], true);
    } else {
      //check for delete announcement
      if (isset($this->request->data['deleteBtn'])) {
        $this->delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Announcement->set($data);
      if ($this->Announcement->validates()) {
        $data["Announcement"]["start_date"] = $this->Util->formatDate($data["Announcement"]["start_date"]);
        $data["Announcement"]["end_date"] = $this->Util->formatDate($data["Announcement"]["end_date"]);
        if (!empty($data["Announcement"]["image_path_1"]) && !empty($data["Announcement"]["image_path_1"]['name'])) {
          $imageUploaded = $this->Upload->uploadImage($data["Announcement"]["image_path_1"], WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' .Configure::read('App.announcementsImageFolder') );
          $data["Announcement"]["image_path_1"] = Configure::read('App.announcementsImageFolder') . '/' . $imageUploaded;
        } else {
          if($action==='add') {
            $data["Announcement"]["image_path_1"] = null;
          } else {
            $data["Announcement"]["image_path_1"] = isset($data["Announcement"]["_image_path_1"]) ? $data["Announcement"]["_image_path_1"] : null;
          }
        }
        if ($this->Announcement->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          //getting previous image
          $data['Announcement']['image_path_1']=$data["Announcement"]["_image_path_1"];
          $data['Announcement']['id']=$id;
        }
        $this->request->data['Announcement'] = $data['Announcement'];
        $this->set('errors', $this->Announcement->validationErrors);
      }
      if($action==='add') {
        $this->render('member_add');
      } else {
        $this->render('member_edit');
      }
  }

  //admin
  function member_delete($id) {
    $this->member_deleteImage($id, false);
    if ($this->Announcement->delete($id)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

  //admin
  function member_deleteImage($id, $redirect=true) {
    $image = $this->Announcement->read('image_path_1', $id);
    $imagePath = $image['Announcement']['image_path_1'];
    if (!empty($imagePath)) {
      $result = $this->Util->deleteFile(WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $imagePath);
      $this->Announcement->save(array('Announcement' => array('id' => $id, 'image_path_1' => '')), false, array('image_path_1'));
      if($redirect){
        $this->Session->setFlash('The image has been deleted.');
        $this->redirect(array('action' => 'edit', $id));
      }
    }
  }

}

?>
